﻿Public Class BackgroundWorkers
    Public Sub BuildWorker_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs)
        Globals.Compiler.StartMake()
    End Sub

    Public Sub BuildWorker_ReportProgress(ByVal sender As System.Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs)
        MainForm.ConsoleOutput.ConsoleText.Text = Globals.Compiler.BuildOutput
    End Sub

    Public Sub BuildWorker_RunWorkerCompleted(ByVal sender As System.Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs)
        Globals.Compiler.EndMake()

        MainForm.ProgressBarStatusBar.Visible = False

        MainForm.ConsoleOutput.ConsoleText.Text = Globals.Compiler.BuildOutput
        MainForm.ConsoleOutput.ConsoleText.SelectionStart =
            MainForm.ConsoleOutput.ConsoleText.Text.Length - 1

        MainForm.ConsoleOutput.ConsoleText.SelectionLength = 0

        MainForm.ConsoleOutput.ConsoleText.ScrollToCaret()

        ' Check for a successful build and return according...
        If Globals.Compiler.Errors.Count > 0 Then
            MainForm.BuildMessageStatusLabel.Text = "Failed to Build"
        Else
            MainForm.BuildMessageStatusLabel.Text = "Build Successful"
        End If

        If Not Globals.Compiler.HasErrors And Globals.ExecuteStandaloneOnBuildFinished Then
            Globals.Run.RunStandalone()
            Globals.ExecuteStandaloneOnBuildFinished = False
        End If
    End Sub

    Public Sub UnrealParserWorker_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs)
        If Not IsNothing(Globals.CurrentProject) Then
            Dim proj As Project.Project = Globals.CurrentProject
            For Each projfile In proj.FileList.ProjectFiles
                projfile.ParseUnrealScript()
            Next
        End If
    End Sub

    Public Sub UnrealParserWorker_RunWorkerCompleted(ByVal sender As System.Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs)
        MainForm.ClassView.Nodes.Clear()
        If Not IsNothing(Globals.CurrentProject) Then
            Dim proj As Project.Project = Globals.CurrentProject

            For Each projfile In proj.FileList.ProjectFiles
                MainForm.ClassView.Nodes.Add(projfile.UnrealClass.RootNode)
            Next

            MainForm.ShowClassBrowserToolStripMenuItem.Enabled = True
        Else
            MainForm.ShowClassBrowserToolStripMenuItem.Enabled = False
        End If

        If Not IsNothing(MainForm.ParserStatusProgressBar) Then
            MainForm.ParserStatusProgressBar.Visible = False
            MainForm.ParserStatusLabel.Text = "Finished Parsing UnrealScript"
        End If

    End Sub
End Class
